package com.example.app;

import android.content.Intent;
import android.os.Bundle;

import com.example.app.HomePage;
import com.example.app.adapter.MMAdapter;
import com.example.app.data.MMissionBean;
import com.example.app.database.DBMissionData;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MyMission extends AppCompatActivity implements View.OnClickListener{

    ViewPager2 viewPager;
    private int addCount=1;
    private TextView textLeft,textRight;
    private Button btn_back,btn_add,btn_del,btn_addHand;
    private DBMissionData data;
    private ArrayList<Fragment>fragments=new ArrayList<>();
    private MyFragmentPagerAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_mission);

        viewPager=findViewById(R.id.mm_vp);
        fragments.add(MMFragment1.newInstance());
        fragments.add(MMFragment2.newInstance("已检查"));
        initPager();
        initTabView();

    }



    private void initTabView() {
        textLeft=findViewById(R.id.mm_btn_l);
        textLeft.setOnClickListener(this);
        textRight=findViewById(R.id.mm_btn_r);
        textRight.setOnClickListener(this);
        btn_back=findViewById(R.id.mm_back);
        btn_back.setOnClickListener(this);
        btn_add=findViewById(R.id.mm_add);
        btn_add.setOnClickListener(this);
        btn_del=findViewById(R.id.mm_del);
        btn_del.setOnClickListener(this);
        btn_addHand=findViewById(R.id.mm_addHand);
        btn_addHand.setOnClickListener(this);

        textLeft.setSelected(true);
    }

    private void initPager() {
        pagerAdapter=new MyFragmentPagerAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    private void changeTab(int position) {
        switch(position)
        {
            case R.id.mm_btn_l:
            case 0:
                viewPager.setCurrentItem(0);
                textLeft.setSelected(true);
                textRight.setSelected(false);
                break;
            case R.id.mm_btn_r:
            case 1:
                viewPager.setCurrentItem(1);
                textLeft.setSelected(false);
                textRight.setSelected(true);
                break;
            case R.id.mm_back:
                finish();
                break;
            case R.id.mm_add:
                data=new DBMissionData(this);
                int t=addCount%5+1;
                data.add(addCount,"第"+addCount+"个任务","第"+addCount+"个地点",t+"级",null,0);
                addCount++;
                initPager();
                break;
            case R.id.mm_addHand:
                startActivity(new Intent(this,AddMission.class));
                finish();
                break;
            case R.id.mm_del:
                data=new DBMissionData(this);
                data.deleteAll();
                addCount=1;
                initPager();
                break;
        }
    }


    @Override
    public void onClick(View view) {
        changeTab(view.getId());
    }
}

